package service;

import beans.GameMap;
import beans.boxPath.Stage;
import beans.boxPath.StageQueue;
import beans.errorInfo.ResponseGenerator;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

import static algorithm.BoxAstarPath.peopleMoveInfo;

/**
 * @Author:Zzy
 * @Date: Crated in 2017-11-28 15:31
 * @Description:
 */

@WebServlet(name = "aaa", urlPatterns = "/getPath")
public class MoveInfoServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("uft-8");
        String mapJson = req.getParameter("mapInfo");
        String boxInfo = req.getParameter("boxInfo");
        String peopleInfo = req.getParameter("peopleInfo");
        int[][] map = null;
        GameMap.initStage(map);
        Stage targetStage = peopleMoveInfo();
        ResponseGenerator r = new ResponseGenerator();
        System.out.println();
        System.out.println();
        System.out.println("查找结果：");
        if (targetStage == null) {
            System.out.println("无解！");
            r.setCode("0");
        } else {
            r.setCode("1").setBody(targetStage.getStepDetail());
            System.out.println("找到最优路径：" + targetStage.getStepDetail());
            System.out.println("共计步数：" + targetStage.getStepCount());
            System.out.println("共计搜寻数：" + StageQueue.stageCount);
        }
        resp.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        out.print(r);

    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void init() throws ServletException {
        super.init();
    }
}

